//
//  BIDSetPasswordForRegisterViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/5/16.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDSetPasswordForRegisterViewController.h"
#import "BIDLoginViewController.h"

//注册
static NSString *registerURL = @"mobile/register.action";
//重置密码
static NSString *resetPasswordURL = @"memberAndroid/resetPwd.action";

@interface BIDSetPasswordForRegisterViewController ()<UIAlertViewDelegate>

@end

@implementation BIDSetPasswordForRegisterViewController
@synthesize phoneNumberForRegister;
@synthesize passwordAction;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    _passwordTF.inputAccessoryView = self.toolBar;
    _confirmPasswordTF.inputAccessoryView = self.toolBar;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 切换明暗文

 @param sender <#sender description#>
 */
- (IBAction)showPasswordBtnHandler:(UIButton *)sender
{
    NSString *strImgName = @"inchoose.png";
    sender.selected = !sender.selected;
    if(sender.selected)
    {
        //选中
        strImgName = @"choosed.png";
        NSString *strPassword = _passwordTF.text;
        _passwordTF.text = @"";
        _passwordTF.secureTextEntry = NO;
        _passwordTF.text = strPassword;
        //
        NSString *strConfirmPassword = _confirmPasswordTF.text;
        _confirmPasswordTF.text = @"";
        _confirmPasswordTF.secureTextEntry = NO;
        _confirmPasswordTF.text = strConfirmPassword;
    }
    else
    {
        //未选中
        strImgName = @"inchoose.png";
        NSString *strPassword = _passwordTF.text;
        _passwordTF.text = @"";
        _passwordTF.secureTextEntry = YES;
        _passwordTF.text = strPassword;
        //
        NSString *strConfirmPassword = _confirmPasswordTF.text;
        _confirmPasswordTF.text = @"";
        _confirmPasswordTF.secureTextEntry = YES;
        _confirmPasswordTF.text = strConfirmPassword;
    }
    [_showPasswordBtn setImage:[UIImage imageNamed:strImgName] forState:UIControlStateNormal];
}

- (IBAction)finishBtnHandler:(id)sender
{
    [_passwordTF resignFirstResponder];
    [_confirmPasswordTF resignFirstResponder];
    //
    NSString *strPassword = _passwordTF.text;
    NSString *strConfirmPassword = _confirmPasswordTF.text;
    if(strPassword.length==0)
    {     
        [self showAlertView:@"密码不能为空" buttonTitle:ALERT_BTN_TITLE tag:@0 functionName:nil];
        return;
    }
    //正则验证是否遵循密码规则
    BOOL bRight = [BIDGlobalMethods checkPassword:strPassword];
    if(!bRight)
    {
        [self showAlertView:@"密码不符合规则" buttonTitle:ALERT_BTN_TITLE tag:@0 functionName:nil];
        return;
    }
    if(![strPassword isEqualToString:strConfirmPassword])
    {
        [self showAlertView:@"两次输入的密码不一致" buttonTitle:ALERT_BTN_TITLE tag:@0 functionName:nil];
        return;
    }
    NSString *strRequestURL = @"";
    NSString *strPost = @"";
    NSString *strSuccessMsg = @"";
    switch(passwordAction)
    {
        case ACTION_RESET:
        {
            strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], resetPasswordURL];
            strSuccessMsg = @"密码重置成功，请返回登录页登录";
            strPost = [[NSString alloc] initWithFormat:@"memberDetail.userName=%@&memberDetail.password=%@", phoneNumberForRegister, strPassword];
        }
            break;
        case ACTION_REGISTER:
        {
            strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], registerURL];
            strSuccessMsg = @"注册成功，请返回登录页登录";
            strPost = [[NSString alloc] initWithFormat:@"member.userName=%@&member.password=%@", phoneNumberForRegister, strPassword];
        }
            break;
    }
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            [self showAlertView:strSuccessMsg buttonTitle:ALERT_BTN_TITLE tag:@100 functionName:@"alertViewHandler:"];
        }
    }];
}

- (void)alertViewHandler:(id)obj
{
    obj = (NSNumber *)obj;
    if([obj unsignedIntegerValue] == 100)
    {
        BIDLoginViewController *vc = [[BIDLoginViewController alloc] initWithNibName:@"BIDLoginViewController" bundle:nil];
        [self.navigationController setViewControllers:@[vc] animated:YES];
    }
}


@end
